; (function (content) {

  class Promise {
    constructor(excutor) {
      const _this = this
      const PENDING = 'pending'
      const RESOLVED = 'resolved'
      const REJECTED = 'rejected'
      this.status = PENDING
      this.data = null
      this.callbacks = []

      function resolve (vlaue) {
        if (_this.status !== PENDING) return
        _this.status = RESOLVED
        _this.data = vlaue
        if (_this.callbacks.length > 0) {
          setTimeout(() => {
            _this.callbacks.forEach(callbacksObj => {
              callbacksObj.onResolved(vlaue)
            })
          })
        }
      }
      function reject (reason) {
        if (_this.status !== PENDING) return
        _this.status = REJECTED
        _this.data = reason
        if (_this.callbacks.length > 0) {
          setTimeout(() => {
            _this.callbacks.forEach(callbacksObj => {
              callbacksObj.onRejected(reason)
            })
          })
        }
      }

      try {
        excutor(resolve, reject)
      } catch (error) {
        reject(error)
      }
    }

    /* 
    Promise上的原型方法
    */
    // 指定成功和失败的回调函数 返回一个新的promise对象  onResolved：成功的回调  onRejected：失败的回调
    then (onResolved, onRejected) {
      const _this = this
      if (typeof onResolved !== 'function') {
        onResolved = value => value
      }
      if (typeof onRejected !== 'function') {
        onRejected = reason => { throw reason }
      }
      return new Promise((resolve, reject) => {
        function handle (callback) {
          try {
            const result = callback(_this.data)
            if (result instanceof Promise) {
              result.then(resolve, reject)
            } else {
              resolve(result)
            }
          } catch (error) {
            reject(error)
          }
        }
        if (_this.status === 'pending') {
          _this.callbacks.push({
            onResolved (value) {
              handle(onResolved)
            },
            onRejected (reason) {
              handle(onRejected)
            }
          })
        } else if (_this.status === 'resolved') {
          setTimeout(() => {
            handle(onResolved)
          })
        } else {
          setTimeout(() => {
            handle(onRejected)
          })
        }
      })
    }

    // 指定失败的回调函数 返回一个新的promise对象
    catch (onRejected) {
      return this.then(undefined, onRejected)
    }

    /* 
      Promise函数对象上的方法 函数对象 => 类对象 添加方法
    */
    // 返回一个指定结果的promise对象
    // 调用Promise.resolve 有可能成功 有可能失败
    // 失败：传递一个失败的promise 就是失败
    static resolve (value) {
      return new Promise((resolve, reject) => {
        if (value instanceof Promise) {
          value.then(resolve, reject)
        } else {
          resolve(value)
        }
      })
    }

    // 返回一个指定reason的失败的promise
    // 调用Promise.reject 必然返回的是一个失败的promise
    static reject (reason) {
      return new Promise((resolve, reject) => {
        reject(reason)
      })
    }

    // 返回一个promise，只有当所有的promise都成功时才成功，否则只要有一个失败的就失败
    // 数组中有可能非promise的值 那么直接成功
    static all (promises) {
      let values = new Array(promises.length)
      let resolvedCount = 0
      return new Promise((resolve, reject) => {
        promises.forEach((p, index) => {
          Promise.resolve(p).then(value => {
            resolvedCount++
            values[index] = value
            if (resolvedCount === promises.length) {
              resolve(values)
            }
          }, reason => {
            reject(reason)
          })
        })
      })
    }

    // 返回一个promise，其结果由第一个完成的promise决定
    static race (promises) {
      return new Promise((resolve, reject) => {
        promises.forEach(p => {
          Promise.resolve(p).then(value => {
            resolve(value)
          }, reason => {
            reject(reason)
          })
        })
      })
    }

    // 返回一个promise对象 它在指定的时间后才确定结果
    static resolvedDelay (value, time) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          if (value instanceof Promise) {
            value.then(resolve, reject)
          } else {
            resolve(value)
          }
        }, time)
      })
    }

    // 返回一个promise对象，它在指定的时间后状态为rejected
    static rejectedDelay (reason, time) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          reject(reason)
        }, time);
      })
    }
  }
})(window)